<?php
/**
 * Created by Yevgen Vershynin.
 * User: Yevgen Vershynin
 * Description: Model of Product entity. Responsible for creation, deleting and fetching Product information from DB.
 */
class ProductModel
{
    public function getProductById($productId)
    {
        $db = DbComponent::getInstance();
        //Query Product entry by ProductNumber from DB
        $rawData = $db->queryRow("SELECT * FROM products WHERE productCode = '".$productId."'");

        if(!empty($rawData))
        {
            $product = new ProductObject($rawData);
        }
        else
        {
            $product = array("error"=>"Wrong product code.");
        }

        return $product;
    }

    public function getAllProducts()
    {
        $db = DbComponent::getInstance();
        //Query all Product entries by ProductNumber from DB
        $rawData = $db->queryAll("SELECT * FROM products");

        if(!empty($rawData))
        {
            foreach($rawData as $productData)
            {
                $products[] = new ProductObject($productData);
            }
        }
        else
        {
            $products = array("error"=>"There is no products");
        }

        return $products;
    }

    public function createProduct($productData)
    {
        //Create product object from available data
        $product = new ProductObject($productData);
        $db = DbComponent::getInstance();
        //Insert data into DB.
        $affectedRows = $db->insert(
            "INSERT INTO products(productCode, productName, productLine, productScale, productVendor,
            productDescription, quantityInStock, buyPrice, MSRP)
            VALUES('$product->code', '$product->name','$product->productLine','$product->scale',
            '$product->vendor','$product->description','$product->quantityInStock','$product->buyPrice',
            '$product->msrp')
        ");

        return ($affectedRows > 0);
    }

    public function updateProduct($productData)
    {
        //Create product object from available data
        $product = new ProductObject($productData);
        if(!empty($product->code))
        {
            $db = DbComponent::getInstance();
            //Update Product entry in DB.
            $affectedRows = $db->insert(
                "UPDATE products set productName='$product->name', productLine='$product->productLine',
                productScale='$product->scale',productVendor='$product->vendor',
                productDescription='$product->description', quantityInStock='$product->quantityInStock',
                buyPrice='$product->buyPrice', MSRP='$product->msrp'
                WHERE productCode = '$product->code'
            ");

            return ($affectedRows > 0);
        }
        else
        {
            return false;
        }
    }

    public function deleteProduct($productId)
    {
        $db = DbComponent::getInstance();
        //Delete product from DB by Product number.
        $affectedRows = $db->insert("DELETE FROM products WHERE productCode='".$productId."'");
        return ($affectedRows > 0);
    }
}
